<?php
if (empty($_GET['realm'])||empty($_GET['char'])||empty($_GET['prof']))
{
	header('Location: account.php');//if the required values are missing, bounce back to the account page
}

include 'wow.html';
include 'connect_pdo.php';
include 'functions.php';

$realm = $_GET['realm'];
$char = $_GET['char'];
$profRecipe = $_GET['prof'];

include 'vars.php';

//if the character has history, get the recipes that way. Otherwise use the armory (that part could be tricky!)
//but only needs to be implemented when the professions page is linked to the character search
$sqlGetProfs = "SELECT recipes, pro_val FROM char_history_pro p LEFT OUTER JOIN char_history c ON c.char_history_id = p.char_history_id LEFT OUTER JOIN char_summary s ON s.char_id = c.char_id WHERE
upper(name) like upper('$char') and upper(realm) like upper('$realm') and upper(pro_name)
like upper('$profRecipe')
ORDER BY p.char_history_id DESC Limit 0,1 ";//get most recent recipe row for character profession

$STH = $DBH->query($sqlGetProfs);
$result = $STH->fetch();
$resultAr = explode(",",$result[0]);//put the result into an array
$currSkill = $result[1];

echo "<p>All items $char can make with $profRecipe at skill level $currSkill</p>";
echo "<button id='grey'>Show items with a skill up at $currSkill</button>";
echo "<br/><button id='orange'>Show items with a guaranteed skill up at $currSkill</button>";
echo "<br/><button id='blue'>Show items that are rare or better</button>";
echo "<br/><button id='purple'>Show items that are epic or better</button>";
echo "<br/><button id='all'>Show all items</button>";

//process array to extract numbers
foreach ($resultAr as $row)
{
	$start = (stripos($row,'>')+1);//get just the number
	$resultArTrim[]=substr($row,$start);
}

$spells = implode(',',$resultArTrim);//put the list of spells together

//get all the item levels
$sqlGetLvl = "SELECT s.spell_id, i.item_level FROM spell s INNER JOIN spell_item si on si.spell_id = s.spell_id INNER JOIN item i ON si.item_id = i.item_id WHERE s.spell_id in ($spells)";
try{
	$STH = $DBH->prepare($sqlGetLvl);
	$STH->execute();
	$result = $STH->fetchall();
}
catch(PDOException $e) {
	echo $e->getMessage();

}
foreach ($result as $row)
{
	$result_spells[]=$row[0];
	$result_lvls[]=$row[1];//create an array of item levels
}

$combine = array_combine($result_spells, $result_lvls);//merge the arrays so the items are the keys
arsort($combine);//sort the array by item level descending
$resultArTrim= array();//empty the array
$resultArTrim = array_keys($combine);//fill it with the keys (spells) sorted

echo "<p class='footnote'>";

foreach ($resultArTrim as $row)//loop through the array of recipes
{
	$sqlGetItems = "SELECT item.item_id, item.name, item.quality, spell.d1, spell.d2, spell.d3, spell.d4, spell.name FROM spell_item, item, spell WHERE type <> 'reagent' AND spell.spell_id= :recipe AND spell.spell_id = spell_item.spell_id AND item.item_id = spell_item.item_id";
	
	// AND spell.name is not null";
	$sqlGetReagents = "SELECT item_id FROM spell_item WHERE type = 'reagent' AND spell_id= :recipe GROUP BY item_id ORDER BY item_id";
	$sqlCheckItem = "SELECT item_id FROM spell_item WHERE type= 'reagent' AND item_id = :itemId";
	$sqlCheckSpell = "SELECT spell_id, name FROM spell WHERE spell_id = :recipe";
	
	try{
		$STH = $DBH->prepare($sqlGetItems);//get what the spell makes
		$STH->bindParam(':recipe', $row,PDO::PARAM_INT);
		$STH->execute();
		$result = $STH->fetchall(PDO::FETCH_NUM);//get the item details
		$itemId = $result[0][0];
		$itemName = $result[0][1];
		$quality = $result[0][2];
		$d1 = $result[0][3];
		$d2 = $result[0][4];
		$d3 = $result[0][5];
		$d4 = $result[0][6];
	}

	catch(PDOException $e) {
		echo $e->getMessage();

	}

	try{
		$STH = $DBH->prepare($sqlCheckSpell);
		$STH->bindParam(':recipe', $row,PDO::PARAM_INT);
		$STH->execute();
		$resultSpell = $STH->fetchall(PDO::FETCH_NUM);
		$spellName = $resultSpell[0][1];
		//print_r($resultSpell);
		if (empty($resultSpell))
		{
		echo "No item data found for recipe $row<br/>";
		}
	}
	catch(PDOException $e) {
		echo $e->getMessage();

	}

		//if the item is also a reagent it is most likely an enchant and we should show the spell name
		try{
			$STH = $DBH->prepare($sqlCheckItem);
			$STH->bindParam(':itemId', $itemId);
			$STH->execute();
			$checkItem = $STH->rowcount();
		}
		catch(PDOException $e) {
			echo $e->getMessage();
		}
		if ($checkItem>0||empty($itemId))
		{
			//our item is also a reagent, use the spell valuess

			if ($spellName<>'')
			{$itemName = $spellName;}
			//if the spell name is empty, not much point in using it
			$enchant = True;
			$spellId = $row;

		}
		else
		{
			$enchant = False;
		}
		
		//reagents
		$STH = $DBH->prepare($sqlGetReagents);
		$STH->bindParam(':recipe', $row,PDO::PARAM_INT);
		$STH->execute();
		$resultReagents = $STH->fetchAll();//get all the reagents

		//spell difficulty block
		$diffClass = 'diff';
		if ($d2<=$currSkill)//if the yellow number is passed already we'll want to hide this
			{
			$diffClass = 'diff hideOrange';
		}
		if ($d4<=$currSkill)//if the grey number is passed already we'll want to hide this
			{
			$diffClass = 'diff hideGrey';
		}

		$diffClass .= ' i'.$quality;

		//hide zero values
		$k=1;
		while ($k<=4)//check difficulty levels
		{
			if (${'d'.$k}==0)
				{${'d'.$k}='';}
			$k++;
		}

		if ($d4<>'')//if the grey value is valid there is difficulty to show
			{
			if (!empty($d1))
			{$span = trim("(<span class='d1'>$d1</span> <span class='d2'>$d2</span> <span class='d3'>$d3</span> <span class='d4'>$d4</span>)");//need to trim the blank spaces out
			}
			else
			{$span = trim("(<span class='d2'>$d2</span> <span class='d3'>$d3</span> <span class='d4'>$d4</span>)");}
			$diff = trim("<span class='$diffClass'>$span</span>");
		}

		//display item name, wowhead link and difficulty
		if ($enchant==True)
			{$rel='spell='.$spellId;}
		else
			{$rel='item='.$itemId;}

		echo "<span class='$diffClass'><a target='_blank' class='footnote q$quality' href='http://www.wowhead.com/$rel' rel='$rel&amp;lvl=$level'>$itemName $diff</a>";

		//this block fetches all the reagents. We need to get the icons for them.
		$sqlGetCraftedReagents = "SELECT item_id FROM item WHERE item_id IN (SELECT item_id FROM spell_item WHERE TYPE = 'product') AND item_id IN (SELECT item_id FROM spell_item WHERE TYPE = 'reagent')";
		$STH = $DBH->prepare($sqlGetCraftedReagents);
		$STH->execute();
		$craftedReagents = $STH->fetchAll();
		
		foreach ($craftedReagents as $reagentItem)
			{
			$reagent_ar[] = $reagentItem[0];
			
		}
				
		//if we do this with PDO it should cache better
		$sqlGetReagentImg = "SELECT url,name FROM item WHERE item_id = :item";
		$STH = $DBH->prepare($sqlGetReagentImg);

		foreach ($resultReagents as $reagent)//loop through reagents
			{
			$STH->bindParam(':item', $reagent[0],PDO::PARAM_INT);
			$STH->execute();
			$resultIcon = $STH->fetch();
			$reagentUrl = $resultIcon[0];
			$reagentName = $resultIcon[1];
			if (in_array($reagent[0],$reagent_ar))//if the reagent is in the list of crafted reagents
				{
				$reagentClass = 'crafted';
			}
			else
				{
				$reagentClass = 'reagent';
			}
			
			echo item_img($reagentUrl,$reagentClass,$reagentName,$reagent[0])." ";
		}
		echo "<br/></span>";//end the span for hiding and the line



}//end loop
$DBH = null;//close the database handle

echo "</p>";
include 'footer.html';






?>